package com.zw.yzk.kabu.management;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.JsonParseException;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.data.constant.Constant;
import com.zw.yzk.kabu.data.exception.CustomException;

import org.json.JSONException;

import java.net.ConnectException;
import java.text.ParseException;

import retrofit2.adapter.rxjava.HttpException;

/**
 * Created by zhanwei on 2016/12/29.
 */

public class ExceptionHandle {

    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static CustomException handleException(Throwable e, Context context) {
        if (e instanceof CustomException) {
            CustomException exception = (CustomException) e;
            String msg = "";
            if (TextUtils.isEmpty(exception.getErrMsg())) {
                switch (exception.getErrCode()) {
                    case Constant.PARSE_ERROR:
                        msg = context.getResources().getString(R.string.parse_error);
                        break;
                    case Constant.UNKNOWN_HOST:
                        msg = context.getResources().getString(R.string.unknown_host_error);
                        break;
                    case Constant.SOCKET_ERROR:
                        msg = context.getResources().getString(R.string.socket_error);
                        break;
                    case Constant.SOCKET_TIMEOUT:
                        msg = context.getResources().getString(R.string.socket_timeout);
                        break;
                    case Constant.IO_ERROR:
                        msg = context.getResources().getString(R.string.io_error);
                        break;
                    case Constant.UPLOAD_QINIU_ERROR:
                        msg = context.getResources().getString(R.string.upload_qiniu_error);
                        break;
                    default:
                        break;
                }
                exception.setErrMsg(msg);
            }
            return exception;
        } else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            CustomException exception = new CustomException();
            switch (httpException.code()) {
                case UNAUTHORIZED:
                case FORBIDDEN:
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                    exception.setErrCode(httpException.code());
                    break;
                default:
                    exception.setErrCode(Constant.NETWORK_ERROR);
                    break;
            }
            return exception;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            return new CustomException(Constant.PARSE_ERROR, context.getResources().getString(R.string.parse_error));
        } else if (e instanceof ConnectException) {
            return new CustomException(Constant.PARSE_ERROR, context.getResources().getString(R.string.connect_error));
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            return new CustomException(Constant.PARSE_ERROR, context.getResources().getString(R.string.ssl_error));
        } else {
            return new CustomException(Constant.UNKNOWN_ERROR);
        }
    }
}